package com.jmu.edu.IMApplication.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Stars
 */
public class AlbumUtils {

    private static List<File> fileList=new ArrayList<>();
    /**
     * 获取图片bitmap
     *
     * @param context
     * @return
     */
    public static List<Bitmap> getPictureBitmap(Context context) {
        List<Bitmap> bitmapList = new ArrayList<>();
        List<String> Paths = getPicturePath(context);
        for (int i = Paths.size() - 1; i > 0; i--) {
            Bitmap bitmap = ImageTools.getBitmap(Paths.get(i));
            bitmapList.add(bitmap);
        }
        return bitmapList;
    }


    /**
     * 获取图片path路径
     *
     * @param context
     * @return
     */
    public static List<String> getPicturePath(Context context) {
        List<String> paths = new ArrayList<>();
        Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//获取照片地址uri
        ContentResolver contentResolver = context.getContentResolver();//创建内容解析者
        Cursor cursor = contentResolver.query(ImagesUri, null  //数据库查询
                , MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?"
                , new String[]{"image/jpeg", "image/png"}
                , MediaStore.Images.Media.DATE_MODIFIED);
        //移动到近10张照片
        if (cursor.getCount() > 100) {
            cursor.moveToPosition(cursor.getCount() - 100);
        }
        while (cursor.moveToNext()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            paths.add(path);
        }
        cursor.close();
        return paths;
    }

    /**
     *
     * @param context
     * @param cursor
     * @param num 加载的数量
     * @return
     */
    public static  List<String> getPicturePath(Context context,Cursor cursor,int num){
        List<String> paths = new ArrayList<>();
        if(cursor == null){
            Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//获取照片地址uri
            ContentResolver contentResolver = context.getContentResolver();//创建内容解析者
            cursor = contentResolver.query(ImagesUri, null  //数据库查询
                    , MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?"
                    , new String[]{"image/jpeg", "image/png"}
                    , MediaStore.Images.Media.DATE_MODIFIED);
        }
        if(cursor.getCount()>num){
            cursor.moveToPosition(cursor.getCount()-num-1);
        }
        while (cursor.moveToNext()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            paths.add(path);
        }
        cursor.close();
        return paths;
    }

    /**
     * 获取图库File
     *
     * @param context
     * @return
     */
    public static List<File> getPictureFile(Context context) {
        List<String> paths = getPicturePath(context);
        List<File> fileList = new ArrayList<>();
        for (int i = paths.size() - 1; i > 0; i--) {
            File file = new File(paths.get(i));
            fileList.add(file);
        }
        return fileList;
    }

    public static List<File> getPictureFile(Context context,Cursor cursor,int num){
        List<String> paths = getPicturePath(context,cursor,num);
        for (int i = paths.size() - 1; i >=0; i--) {
            File file = new File(paths.get(i));
            fileList.add(file);
        }
        return fileList;
    }

}
